<script src="<?php echo $base_url?>js/jquery-1.4.4.js" type="text/javascript"></script>
<script type="text/javascript">
function memProfile(member) {
	window.location='index.php?members='+member;
}
function editMemProfile(member) {
	window.location='index.php?editmem='+member;
}
function funDelMem(userId) {
	var delPro=confirm("Are you sure want to delete this member?");
	if(delPro==true) {
		var frm = document.getElementById('form1');
		document.getElementById("delMember").value=userId;
		frm.submit();
	}	
}

</script>
<?php
require_once "../includes/pagination.php";

if(!empty($_POST['delMember']))
{
	$memberId=$_POST['delMember'];
	$sqlDelMem="Delete from tbl_users where user_id=$memberId";
	$rstDelMem=mysql_query($sqlDelMem);	
}


$cond = '';
$sort = '';
$ut = $u = $fn = $ln = $dg = 'asc';
if(isset($_GET['sort']))
{
	
	if($_GET['sort']=='user_type')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY user_type ASC';	$sort = '&sort=user_type&s_type=asc';	$ut='desc';	}
		else
		{	$cond=' ORDER BY user_type DESC';	$sort = '&sort=user_type&s_type=desc';	}
	}
	else if($_GET['sort']=='username')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY username ASC';		$sort = '&sort=username&s_type=asc';	$u='desc';	}
		else
		{	$cond=' ORDER BY username DESC';	$sort = '&sort=username&s_type=desc';	}
	}
	else if($_GET['sort']=='first_name')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY first_name ASC';	$sort = '&sort=first_name&s_type=asc';	$fn='desc';	}
		else
		{	$cond=' ORDER BY first_name DESC';	$sort = '&sort=first_name&s_type=desc';	}
	}
	else if($_GET['sort']=='last_name')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY last_name ASC';	$sort = '&sort=last_name&s_type=asc';	$ln='desc';	}
		else
		{	$cond=' ORDER BY last_name DESC';	$sort = '&sort=last_name&s_type=desc';	}
	}
	else if($_GET['sort']=='created_date')
	{
		if($_GET['s_type']=='asc')
		{	$cond=' ORDER BY created_date ASC';	$sort = '&sort=created_date&s_type=asc'; $dg='desc'; }
		else
		{	$cond=' ORDER BY created_date DESC';	$sort = '&sort=created_date&s_type=desc';}
	}
}

?>
<form name="form1" id="form1" method="post" action="">
 <input type="hidden" name="delMember" id="delMember">
			  
<table width="100%"  border="1" cellspacing="0" class="container">
  <tr>
    <td width="469" height="25" class="tdHeader">Buyers  List</td>
  </tr>
  <tr>
    <td height="577">
	<table width="100%">
            <tbody>
		<tr>
			<td>
				<form name="frmSearchPro" action="" method="post">
				<table width="100%" border="0">
					<tr>
						<td width="20%">Date Registered</td>
						<td width="30%">
							<input type="text" name="created_date" id="created_date" style="width: 150px;" value="<?php echo $_POST['created_date']?>" />
						</td>
						<td width="20%">Username</td>
						<td width="30%"><input type="text" name="username" id="username" style="width: 150px;" value="<?php echo $_POST['username']?>" /></td>
					</tr>
					<tr>
						<td width="20%">First Name</td>
						<td width="30%">
							<input type="text" name="first_name" id="first_name" style="width: 150px;" value="<?php echo $_POST['first_name']?>" />
						</td>
						<td width="20%">Last Name</td>
						<td width="30%"><input type="text" name="last_name" id="last_name" style="width: 150px;" value="<?php echo $_POST['last_name']?>" /></td>
					</tr>

					<tr>
						<td height="35" colspan="5" style="text-align:center"><input type="submit" name="btnSearchPro" value="Search Member" /></td>
					</tr>
				</table>
				</form>
			</td>
		</tr>
		<tr>
			<td>
				<b>Sort  By :</b>
				<a href="<?php echo $base_url;?>admin/index.php?buyer&sort=user_type&s_type=<?php echo $ut;?>">Member Type</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?buyer&sort=username&s_type=<?php echo $u;?>">Username</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?buyer&sort=first_name&s_type=<?php echo $fn;?>">First name</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?buyer&sort=last_name&s_type=<?php echo $ln;?>">Last name</a> &nbsp;&nbsp;&nbsp;
				<a href="<?php echo $base_url;?>admin/index.php?buyer&sort=created_date&s_type=<?php echo $dg;?>">Date Regd.</a>
			</td>
		</tr>
        </tbody></table>
	<br>
	<table width="100%"  border="0" cellspacing="0">      
	<tr class="InnerHeader">       
		<td width="16%" class="line">Member type </td>
		<td width="14%" class="line">Username </td>
		<td width="13%" class="line">First name </td>
		<td width="15%" class="line">Last name </td>
		<td width="17%" class="line">Date Regd.</td>
		<td width="8%" class="line">&nbsp;</td>
		<td width="7%" class="line">&nbsp;</td> 
		<td width="10%" class="line">&nbsp;</td>         
	</tr>
	<?php 				 
	
	$itemPerPage=15;
	$search='';
	if(isset($_POST['btnSearchPro'])) {
		
		$username 	= $_POST['username'];		
		$created_date 	= $_POST['created_date'];
		$first_name 	= $_POST['first_name'];
		$last_name 	= $_POST['last_name'];
		
		if($created_date!='')
		$search.= " AND created_date='".$created_date."'";
		
		if($user_type!='')
		$search.=" AND username='".$username."'";
		
		if($first_name!='')
		$search.="AND first_name='".$first_name."'";
		
		if($last_name!='')
		$search.="AND last_name='".$last_name."'";
		
	}
	
	$sql_shocat="SELECT * FROM tbl_users where status!=0 AND status!=2 AND user_type=1 $search $cond";				 
	$rst_shocat=mysql_query(getPagingQuery($sql_shocat,$itemPerPage));
	$numRows = mysql_num_rows($rst_shocat);
	if($numRows>0) {				
		while($row_shocat = mysql_fetch_array($rst_shocat)) {				 
		$mem_id=$row_shocat['user_id'];
		$mem_name=$row_shocat['username'];
		$usr_type=$row_shocat['user_type'];
		$created_date=$row_shocat['created_date'];
		if($usr_type==1)
		       $mem_type = "Buyer";
	       else if($usr_type==2)
		       $mem_type = "Seller";		 
	       else if($usr_type==3)
		       $mem_type = "Merchant";	
	       else if($usr_type==4)
		       $mem_type = "Seller/ Buyer";
	       else if($usr_type==5)
		       $mem_type = "Wholeseller";
			       
		$mem_fname=$row_shocat['first_name'];
		$mem_lname=$row_shocat['last_name'];
		echo "<tr>";
		echo "<td class='line'>".ucfirst($mem_type)."</td>";
		echo "<td class='line'>$mem_name</td>";
		echo "<td class='line'>$mem_fname</td>";
		echo "<td class='line'>$mem_lname</td>";
		echo "<td class='line'>$created_date</td>";
		echo "<td height='20' class='line'><input type='button' name='edit' value='Edit' class='button'  onclick='editMemProfile($mem_id)'></td> ";
		echo "<td height='20' class='line'><input type='button' name='view' value='View' class='button'  onclick='memProfile($mem_id)'></td> ";
		echo "<td height='20' class='line'><input type='button' name='delete' value='Delete' class='button'  onclick='funDelMem($mem_id)';></td> ";
		echo "</tr>";				
		}
	} else {
	       echo "<tr>
			       <td colspan='8' class='line'>No Members found.</td>
			 </tr>";
	}				 
		?>      
       </table>
	<div align="center">
		<?php		   
		   $link="members";
		   echo $disp=getPagingLink($sql_shocat,$itemPerPage,$link,$sort);							   
		?>    
	</div>
	</td>
 </tr>
</table>